/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;


import Merlion.CRM.bean.MessageManagerBeanRemote;
import Merlion.userManagement.entity.MessageEntity;

import Merlion.userManagement.entity.SystemUserEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;


@ManagedBean(name = "MessageManagedBean")
@ViewScoped
public class MessageManagedBean implements Serializable {

    private String receiverId;
    private String title;
    private String body;
    private List<MessageEntity> msgList;
    private MessageEntity selectedMessage;
    private MessageEntity selectedViewMessage;
    private MessageEntity[] selectedMessages;
    private List<SystemUserEntity> receiverList;
    private boolean readd;
    private List<String> userSuggestions;
    private List<MessageEntity> sentMsgList;
    @EJB
    private MessageManagerBeanRemote mmb;

    
    
    /** Creates a new instance of MessageManagedBean */
    public MessageManagedBean() {
    }
    
    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (msgList == null) {
            msgList = mmb.viewMessage();
        }
    }

    public List<SystemUserEntity> getReceiverList() {
        if (receiverList == null) {
            receiverList = mmb.getUsers();
        }
        return receiverList;
    }

    public void setReceiverList(List<SystemUserEntity> receiverList) {
        this.receiverList = receiverList;
    }

    public boolean isReadd() {
        return readd;
    }

    public void setReadd(boolean readd) {
        this.readd = readd;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getReceiverId() {
        return receiverId;
    }

    public void setReceiverId(String receiverId) {
        this.receiverId = receiverId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void sendMessage(ActionEvent event) {
        List<String> receivers = new ArrayList<String>();
        receivers.add(receiverId);
        System.out.println("WEB ::  SENDING MESSAGE");
        mmb.addMessage(receivers, title, body);
        System.out.println("Message added");

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("messages.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public List<MessageEntity> getMsgList() {
        if (msgList == null) {
            msgList = mmb.viewMessage();
        }
        return msgList;
    }

    public void setMsgList(List<MessageEntity> msgList) {
        this.msgList = msgList;
    }

    public MessageEntity getSelectedMessage() {
        return selectedMessage;
    }

    public void setSelectedMessage(MessageEntity selectedMessage) {
        this.selectedMessage = selectedMessage;
    }

    public MessageEntity[] getSelectedMessages() {
        return selectedMessages;
    }

    public void setSelectedMessages(MessageEntity[] selectedMessages) {
        this.selectedMessages = selectedMessages;
    }

    public MessageEntity getSelectedViewMessage() {
        return selectedViewMessage;
    }

    public void setSelectedViewMessage(MessageEntity selectedViewMessage) {
        System.out.println("SELECTED VIEW MESSAGE");
        this.selectedViewMessage = selectedViewMessage;
        markAsRead();
    }

    public void markAsRead() {
        if (selectedViewMessage != null) {
            mmb.markAsRead(selectedViewMessage.getId());
        }
    }

    public void delete(ActionEvent event) {
        System.out.println("WEB:: Delete message");
        mmb.deleteMessage(selectedMessage.getId());
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("messages.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteMessages() {
        System.out.println(":::WEB::: Delete messages");

        if (selectedMessages != null) {
            System.out.println(":::WEB::: If selected messages not null");
            for (int i = 0; i < selectedMessages.length; i++) {
                System.out.println("selectedMessages i: " + i + " - " + selectedMessages[i].getId());
                mmb.deleteMessage(selectedMessages[i].getId());
            }
        } else {
            System.out.println(":::WEB::: Selected messages are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("messages.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void cancelDelete() {
        for (MessageEntity client : selectedMessages) {
            client = null;
        }
        if (selectedMessages == null) {
            System.out.println(":::WEB::: Deletion of messages cancelled");

        } else {
            System.out.println(":::WEB::: Deletion of messages are not cancelled");
        }

    }

    public List<String> userSuggestions(String query) {

        List<SystemUserEntity> userList = getReceiverList();
        if (userList != null) {
            System.out.println("Userlist not null");
            for (Object o : userList) {
                SystemUserEntity su = (SystemUserEntity) o;
                String email = su.getEmail();
                String name = su.getName();

                userSuggestions.add(email);
            }
        }
        return userSuggestions;
    }

    public void setUserSuggestions(List<String> userSuggestions) {
        this.userSuggestions = userSuggestions;
    }

    public void checkNewMessage(ActionEvent actionEvent) {
        List<MessageEntity> newMsg = mmb.viewNewMessage();
        if (newMsg != null && !newMsg.isEmpty()) {
            FacesContext context = FacesContext.getCurrentInstance();

            context.addMessage(null, new FacesMessage("Notification", "You have one new message!"));
        }
    }

    public void initViewSent(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (sentMsgList == null) {
            sentMsgList = mmb.viewSentItems();
        }
    }

    public List<MessageEntity> getSentMsgList() {
        if (sentMsgList == null) {
            sentMsgList = mmb.viewSentItems();
        }
        return sentMsgList;
    }

    public void setSentMsgList(List<MessageEntity> sentMsgList) {
        this.sentMsgList = sentMsgList;

    }

}
